import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;

public class ProgressBar extends JPanel implements ActionListener, 
		PropertyChangeListener {

    	private JProgressBar progressBar;
    	private JButton startButton;
    	private JButton stopButton;
    	private JButton clearButton;
    	private Task task;
    	private boolean stop;
    	private int progressState;
    
    	class Task extends SwingWorker<Void, Void> {
        
        	@Override
        	public Void doInBackground() {
	    		while (progressState<100) {
	        		try {
		    			Thread.sleep(100);
				} catch (InterruptedException ignore) {}
                			setProgress(++progressState);
                			if (stop) {
                    			stop = false;
                    			startButton.setEnabled(true);
        	    			stopButton.setEnabled(false);
                    			return null;
				}
	    		}
            		startButton.setEnabled(true);
	    		stopButton.setEnabled(false);
            		return null;
        	}
    	}

    	public ProgressBar() {
        	super(new BorderLayout());
        
        	progressState = 0;
        	stop = false;
        
        	startButton = new JButton("Start");
        	startButton.setActionCommand("start");
        	startButton.addActionListener(this);

        	stopButton = new JButton("Stop");
        	stopButton.setActionCommand("stop");
        	stopButton.addActionListener(this);
		stopButton.setEnabled(false);

        	clearButton = new JButton("Clear");
        	clearButton.setActionCommand("clear");
        	clearButton.addActionListener(this);
        
        	progressBar = new JProgressBar(0, 100);
        	progressBar.setValue(progressState);
        	progressBar.setStringPainted(true);

        	JPanel panel = new JPanel();
        	panel.add(startButton);
        	panel.add(stopButton);
        	panel.add(clearButton);
        	panel.add(progressBar);

        	add(panel, BorderLayout.PAGE_START);
        	setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    	}

     	public void actionPerformed(ActionEvent evt) {
        	if (evt.getActionCommand()=="start"){
	    		startButton.setEnabled(false);
	    		stopButton.setEnabled(true);
            		task = new Task();
            		task.addPropertyChangeListener(this);
            		task.execute();
		} else if (evt.getActionCommand()=="stop"){
            		stop = true;
        	} else if (evt.getActionCommand()=="clear"){
            		progressState = 0;
            		progressBar.setValue(progressState);
        	}
    	}

    
    	public void propertyChange(PropertyChangeEvent evt) {
        	if ("progress" == evt.getPropertyName()) {
            		int progress = (Integer) evt.getNewValue();
            		progressBar.setValue(progress);
       		} 
    	}

    	private static void createAndShowGUI() {
 
        	JFrame frame = new JFrame("ProgressBar 1.0");
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setResizable(false);
        	frame.setLocation(200, 100);
        	        
        	JComponent newContentPane = new ProgressBar();
        	newContentPane.setOpaque(true); 
        	frame.setContentPane(newContentPane);

        	frame.pack();
        	frame.setVisible(true);
    	}

    	public static void main(String[] args) {
  
        	javax.swing.SwingUtilities.invokeLater(new Runnable() {
            	public void run() {
                	createAndShowGUI();
            	}
        });
    }
}
